package data.set.implementation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import data.set.OfflineSet;
import data.set.OfflineSetIterator;


/**
 * Builds an offlineset into memory
 * @author guillaumes
 *
 * @param <A> : the data of the offlineset 
 */
public final class OfflineSet_Memory<A> implements OfflineSet<A>{
	
	private ArrayList<A> objects;
	private ArrayList<Integer> ids;
	private HashMap<Integer,Integer> indexes;
	
	public OfflineSet_Memory()
	{
		objects=new ArrayList<A>();
		ids=new ArrayList<Integer>();
		indexes=new HashMap<Integer,Integer>();
	}
	
	public final  A getObject(int id) {
		Integer in=indexes.get(id);
		if (in==null) return(null);
		return(objects.get(in));
	}

	public final OfflineSetIterator<A> inputsetiterator() {
		return(new OfflineSetIterator_Memory<A>(objects.iterator(),ids.iterator()));
	}

	public final Iterator<A> iterator() {
		return(objects.iterator());
	}

	public final long size() {
		return(objects.size());
	}
	
	public final void add(int id,A object)
	{
		if (!indexes.containsKey(id))
		{
			objects.add(object);
			ids.add(id);
			indexes.put(id, objects.size()-1);
		}
		else
		{			
			objects.set(indexes.get(id),object);		
		}
	}

}
